﻿local L = LibStub( "AceLocale-3.0" ):GetLocale( "SimpleActionSets" )

function SimpleActionSets:ActionBarButton_OnClick( self, button )
	local bar = self:GetParent():GetID();
	local enable = SimpleActionSets:BarEnabled(bar)
	local LocalSavedBar = SimpleActionSets:CopyTable(SimpleActionSets:GetTemp(bar));
	local returnbar = SimpleActionSets:GetDraggingBar();

	if ( button == "RightButton" ) then
		if ( LocalSavedBar ) then
			local set;
			for id = 1, 12 do
				if ( not LocalSavedBar[id] or type(LocalSavedBar[id]) == "number" ) then
					if ( not set ) then
						set = (LocalSavedBar[id] or -1) + 1;
					end
					if ( set == 2 ) then
						SimpleActionSets:SetTemp(bar, id, nil);
					else
						SimpleActionSets:SetTemp(bar, id, set);
					end
				end
			end
		end
		LocalSavedBar = nil;
	elseif ( SASFakeDragFrame.Bar and not IsShiftKeyDown() and not IsControlKeyDown() ) then
		SimpleActionSets:SetTemp(bar, nil, SimpleActionSets:FakeDrag_Drop(1));
		if ( returnbar ) then
			SimpleActionSets:SetDraggingBar(nil);
			SimpleActionSets:SetTemp(returnbar, nil, SimpleActionSets:CopyTable(LocalSavedBar));
			SimpleActionSets:Actions_UpdateBar(returnbar);
		end
		LocalSavedBar = nil;
	elseif ( IsControlKeyDown() and not IsShiftKeyDown() ) then
		SimpleActionSets:SetTemp(bar, nil, {});
	end

	if ( SimpleActionSets:BarHasActions(bar) and LocalSavedBar ) then
		SimpleActionSets:FakeDrag_PickupBar(LocalSavedBar);
		if ( not IsShiftKeyDown() ) then
			SimpleActionSets:SetDraggingBar(bar);
		end
	end
	SimpleActionSets:Actions_UpdateBar(bar);
	SimpleActionSets:Actions_SaveEnable();
end

function SimpleActionSets:ActionBarButton_OnEnter( self )
	self:SetScript("OnUpdate", ActionBarButton_OnUpdate);
	GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
	GameTooltip:SetText( L["SAS_TEXT_BARS_"..self:GetParent():GetID()] );
	GameTooltip:Show();
end

function SimpleActionSets:ActionBarButton_OnLeave( self )
	self:SetScript("OnUpdate", nil);

	for i=1, 12 do
		local barButton = _G[self:GetParent():GetName().."Action"..i]:GetName();
		_G[barButton.."Delete"]:Hide();
		_G[barButton.."Copy"]:Hide();
	end

	GameTooltip:Hide();
end

function SimpleActionSets:ActionBarButton_OnDragStart( self )
	local bar = self:GetParent():GetID();
	if ( SimpleActionSets:BarHasActions(bar) ) then
		SimpleActionSets:Debug("Picking up bar "..bar );
		SimpleActionSets:FakeDrag_PickupBar( SimpleActionSets:GetTemp(bar) );
		SimpleActionSets:SetDraggingBar(bar);
		SimpleActionSets:Actions_UpdateBar(bar);
	else
		SimpleActionSets:FakeDrag_PickupBar();
		SimpleActionSets:SetDraggingBar(nil);
	end
end

function SimpleActionSets:ActionBarButton_OnDragStop()
	self:FakeDrag_Drop();
end

function SimpleActionSets:ActionBarButton_OnReceiveDrag( self )
	SimpleActionSets:ActionBarButton_OnClick( self );
end

function SimpleActionSets:ActionBarButton_OnUpdate( self )
	if ( not SimpleActionSets:GetTemp() ) then return; end

	local BarFrame = self:GetParent();
	local barName = BarFrame:GetName();

	for i=1, 12 do
		local barButton = _G[barName.."Action"..i]:GetName();
		if ( IsControlKeyDown() and not IsShiftKeyDown() and _G[barButton.."Icon"]:IsVisible() ) then
			_G[barButton.."Delete"]:Show();
			_G[barButton.."Copy"]:Hide();
		elseif ( IsShiftKeyDown() and not IsControlKeyDown() and SimpleActionSets:BarHasActions(BarFrame:GetID()) ) then
			_G[barButton.."Copy"]:Show();
			_G[barButton.."Delete"]:Hide();
		else
			_G[barButton.."Delete"]:Hide();
			_G[barButton.."Copy"]:Hide();
		end
	end
end